<%@ page contentType="text/html; charset=UTF-8"%>
<jsp:directive.page import="java.io.File"/>
<jsp:directive.page import="java.util.List"/>
<jsp:directive.page import="java.util.Iterator"/>
<jsp:directive.page import="com.myzion.core.common.util.DateUtils"/>
<jsp:directive.page import="com.myzion.core.common.util.Constants"/>
<jsp:directive.page import="org.apache.commons.lang.StringUtils"/>
<jsp:directive.page import="org.apache.commons.fileupload.FileItem"/>
<jsp:directive.page import="org.apache.commons.fileupload.servlet.ServletFileUpload"/>
<jsp:directive.page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"/>

<%
String message = "";
String fPath = request.getParameter("filePath");
String inputFileName = request.getParameter("inputFileName");
String inputFileSize = request.getParameter("inputFileSize");
fPath = (fPath == null ? "" : fPath);
%>
<%
String fileName = "";
String fileSize = "";
String filefrontname = "";

if(StringUtils.isNotEmpty(request.getParameter("todo"))) {

	//上传文件的目录
	String uploadPath;
	if(fPath.equals("promise") || fPath.equals("requirement") || fPath.equals("report")) {
		uploadPath = application.getRealPath(Constants.getUploadPathForDay(fPath)) + "/";
	} else {
		uploadPath = application.getRealPath(Constants.getUploadPath(fPath)) + "/";
	}
	
	try {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload fileUpload = new ServletFileUpload(factory);
		// 设置最大文件尺寸，这里是3MB
		fileUpload.setSizeMax(3*1024*1024);
		// 设置临时目录：
		fileUpload.setHeaderEncoding("UTF-8");
		
		//得到所有的文件
		List fileItems = fileUpload.parseRequest(request);
		Iterator iter = fileItems.iterator();
		
		//如果文件目录不存在，做目录
		File modelDir = new File(uploadPath);
	    if (!modelDir.exists()) {
	        modelDir.mkdirs();
	    }
	    //依次处理每一个文件
		while (iter.hasNext()) {
        	FileItem item = (FileItem) iter.next();
        	if (!item.isFormField()) {
        	    filefrontname = item.getName().substring(0,item.getName().lastIndexOf("."));
        	    String filetype=item.getName().substring(item.getName().lastIndexOf("."),item.getName().length());
        		fileName = filefrontname + "_" + DateUtils.hms() + filetype;
        		long size = item.getSize();
        		if (StringUtils.isEmpty(fileName) && size == 0) {
        	continue;
        		}
        		int len = fileName.lastIndexOf("\\");
        		fileName = fileName.substring(len + 1);
        		File temp = new File(uploadPath + fileName);
        		item.write(temp);
        		fileSize = size+"";
        	}
        		}
        	} catch(Exception e) {
        		message = "文件上传失败，请重试上传！";
        }
     }
%>        

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> 
<title>File Upload</title>

<script type="text/javascript">
function setValue() {
	<%if(fileName.toString().length() > 1) {%>
	
		var obj = window.opener.document;
		var nameObj = '<%=inputFileName%>';
		var sizeObj = '<%=inputFileSize%>';
		var fnObj = obj.all[nameObj];
		var fsObj = obj.all[sizeObj];
		
		var fnVal = '<%=fileName.substring(0, fileName.length())%>';
		var fsVal = '<%=fileSize.substring(0, fileSize.length()-1)%>';
		
		if(typeof(fnObj) != 'undefined') {
			fnObj.value = fnVal;
		}
		if(typeof(fsObj) != 'undefined') {
			fsObj.value = fsVal;
		}			
	    window.close();
	<%}%>
}
</script>

</head>

<body onLoad="setValue()">
<p style="font-weight:bold; font-size:20px; text-align:center">文件上传</p>
<p><font color="red"><%=message%></font>
<form name="uploadForm" action="fileupload.jsp?todo=upload&filePath=<%=fPath%>&inputFileName=<%=inputFileName%>&inputFileSize=<%=inputFileSize%>" method="post" enctype="multipart/form-data">
  <ul style="list-style-type:none;">
    <li><input type="file" name="upload" size="45"/></li>
    <li><span style="font-size:9pt; color:#ff0000;">*单个附件不能大于3M</span></li>
  </ul>
  
  <p style="text-align:center">
    <input name="button" type="submit" value="上 传"/>
    <input name="button2" type="button" onClick="window.close()" value="取 消"/>
  </p>
</form>
</body>
</html>

